package com.coding.easier.translate;

import com.coding.easier.enums.LanguagesEnum;
import com.coding.easier.ui.modules.ColorService;
import com.coding.easier.ui.modules.IdeaColorService;
import com.intellij.notification.impl.NotificationsManagerImpl;
import com.intellij.openapi.ui.popup.Balloon;
import com.intellij.openapi.ui.popup.JBPopup;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.wm.IdeFrame;
import com.intellij.ui.*;
import com.intellij.ui.awt.RelativePoint;
import com.intellij.ui.components.JBComboBoxLabel;
import com.intellij.ui.components.JBLabel;
import com.intellij.ui.components.JBPanel;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.util.ui.JBEmptyBorder;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: D丶Cheng
 * @description: 翻译ui
 * @create: 2019-09-10 10:56
 **/
class TranslateBalloon {

    private GoogleTranslateResult result;

    private GoogleTranslateAction translateAction;

    static List<String> allLanguages = LanguagesEnum.getAllLanguages();

    private JPanel jPanel;

    static JEditorPane origEditorPane = new JEditorPane();

    static JEditorPane transEditorPane = new JEditorPane();

    private int origLength;

    private int transLength;

    static public int origIndex = 0;

    static public int transIndex = 1;

    private int height;

    private int width;

    JPanel getjPanel() {
        return jPanel;
    }

    int getHeight() {
        return height;
    }

    int getWidth() {
        return width;
    }

    static {
        ColorService.install(new IdeaColorService());
    }

    TranslateBalloon(GoogleTranslateAction translateAction, GoogleTranslateResult result) {
        this.translateAction = translateAction;
        this.result = result;
        init();
    }

    private void init() {
        jPanel = new JPanel(new BorderLayout());
        jPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        StringBuilder origBuilder =
                new StringBuilder("<body style='font-size:14px;font-weight: bold;color: #CAA923;'>");
        StringBuilder transBuilder =
                new StringBuilder("<body style='font-size:14px;font-weight: bold;color: #CAA923;'>");
        if (result.getSentences().size() == 1 || result.getSentences().size() == 2) {
            GoogleTranslateResult.SentencesBean sentencesBean = result.getSentences().get(0);
            origBuilder.append(result.getSentences().get(0).getOrig());
            if (result.getSentences().size() == 2 && result.getSentences().get(1).getSrcTranslit() != null) {
                origBuilder.append("<br>").append(result.getSentences().get(1).getSrcTranslit());
            }
            transBuilder.append(result.getSentences().get(0).getTrans()).append("<br>");
            if (!sentencesBean.getOrig().equals(sentencesBean.getTrans()) && result.getDict() != null) {
                for (int i = 0, len = result.getDict().size(); i < len; i++) {
                    transBuilder.append(result.getDict().get(i));
                }
            }
        }
        else {
            for (GoogleTranslateResult.SentencesBean sentence : result.getSentences()) {
                if (sentence.getTrans() != null) {
                    origBuilder.append(sentence.getOrig());
                    transBuilder.append(sentence.getTrans());
                }
            }
        }
        origBuilder.append("</body>");
        transBuilder.append("</body>");
        origLength = origBuilder.length();
        transLength = transBuilder.length();
        createPanel("原文：", origBuilder, BorderLayout.NORTH, origEditorPane, 0);
        createPanel("结果：", transBuilder, BorderLayout.CENTER, transEditorPane, 1);
    }

    /**
     * @param text
     * @param builder
     * @param position
     * @param editorPane
     * @param type       0 原文  1 翻译结果
     */
    private void createPanel(String text, StringBuilder builder, String position, JEditorPane editorPane, int type) {
        JPanel panel = new JPanel(new BorderLayout());
        JPanel topPanel = createTopPanel(text, type);
        panel.add(topPanel, BorderLayout.NORTH);
        editorPane.setContentType("text/html");
        editorPane.setText(builder.toString());
        editorPane.setEditable(false);
        editorPane.setBackground(ColorService.forCurrentTheme(ColorService.Background));
        editorPane.setSelectionStart(0);
        editorPane.setSelectionEnd(0);
        editorPane.getSelectedText();
        JBScrollPane scrollPane = new JBScrollPane(editorPane);
        scrollPane.setBorder(null);
        scrollPane.setMaximumSize(new Dimension(520, 200));
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        panel.add(scrollPane, BorderLayout.CENTER);
        setPreferredSize(builder.length(), panel);
        jPanel.add(panel, position);
    }

    /**
     * 获取顶部面板
     *
     * @param text
     * @param type 0 原文  1 翻译结果
     * @return
     */
    private JBPanel createTopPanel(String text, int type) {
        ComboBoxModel<String> comboBoxModel = new CollectionComboBoxModel(allLanguages);
        JComboBox jComboBox = new JComboBox(comboBoxModel);
        JBPanel topPanel = new JBPanel(new FlowLayout(FlowLayout.LEFT));
        JBLabel label = new JBLabel(text);
        label.setForeground(new JBColor(new
                Color(16, 187, 100), new Color(16, 187, 100)));
        label.setFont(new
                Font("Microsoft YaHei", Font.BOLD, 16));
        topPanel.add(label);
        if (type == 0) {
            jComboBox.setSelectedIndex(origIndex);
        }
        else {
            jComboBox.setSelectedIndex(transIndex);
        }
        jComboBox.addItemListener(itemEvent -> {
            switch (itemEvent.getStateChange()) {
                case ItemEvent.SELECTED:
//                    System.out.println("选中" + itemEvent.getItem());
                    if (type == 0) {
                        origIndex = jComboBox.getSelectedIndex();
//                        System.out.println("origIndex ===>" + origIndex);
                    }
                    else {
                        transIndex = jComboBox.getSelectedIndex();
//                        System.out.println("transIndex ===>" + transIndex);
                    }
                    translateAction.doTranslate(result.getSentences().get(0).getOrig());
                    break;
                case ItemEvent.DESELECTED:
//                    System.out.println("取消选中" + itemEvent.getItem());
                    break;
                default:
                    break;
            }
        });
        jComboBox.setPreferredSize(new Dimension(153, 30));
        topPanel.add(jComboBox);
        //        JBLabel rightLabel = new JBLabel("测试文字");
        //        topPanel.add(rightLabel);
        return topPanel;
    }

    private void setPreferredSize(int len, JPanel panel) {
        boolean flag = len * 16 > 540;
        int width = flag ? 520 : (len * 16 + 80);
        int height = flag ? Math.min(((len * 16 / 520 + 2) * 16), 200) : 95;
        height = Math.max(height, 95);
        this.height += height;
        this.width = Math.max(this.width, width);
        panel.setPreferredSize(new Dimension(width, height));
    }
}
